package com.mes.cloud.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import com.mes.cloud.dao.BmeBcDAO;
import com.mes.cloud.dao.BmeBcSubDAO;
import com.mes.cloud.dto.BcViewDTO;
import com.mes.cloud.dto.BmeBcDTO;
import com.mes.cloud.dto.BmeBcSubDTO;
import com.mes.cloud.service.BmeBcSubService;
import com.mes.cloud.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * bme_bc_sub基础表
 *
 * @author xhy
 * @version 1.0
 * @date 2019/05/09 16:00
 */
@Service
public class BmeBcSubServiceImpl implements BmeBcSubService {
    @Autowired
    private BmeBcSubDAO bmeBcSubDAO;
    @Autowired
    private BmeBcDAO bmeBcDAO;

    /**
     * 根据条件获取bme_bc_sub基础表列表
     *
     * @param condition
     * @return List<BmeBcSubDTO>
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public List<BmeBcSubDTO> getBmeBcSubList(Map<Object, Object> condition) {
        return bmeBcSubDAO.getBmeBcSubList(condition);
    }


    @Override
    public List<BmeBcSubDTO> findListByBcCode(Map<Object, Object> map) {
        BmeBcDTO bmeBcDTO = bmeBcDAO.getBmeBcByCode(map.get("code").toString());
        Map selectParam = new HashMap();
        selectParam.put("pid",bmeBcDTO.getId());
        return bmeBcSubDAO.getBmeBcSubList(selectParam);
    }

    /**
     * 获取bme_bc_sub基础表的最大记录数
     *
     * @param condition
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public long getBmeBcSubCount(Map<Object, Object> condition) {
        return bmeBcSubDAO.getBmeBcSubCount(condition);
    }

    /**
     * 新增bme_bc_sub基础表
     *
     * @param bmeBcSubDTO
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public long insertBmeBcSub(BmeBcSubDTO bmeBcSubDTO) {
        bmeBcSubDTO.setId(UuId.getShortUuid());
        return bmeBcSubDAO.insertBmeBcSub(bmeBcSubDTO);
    }

    /**
     * 更新bme_bc_sub基础表
     *
     * @param bmeBcSubDTO
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public long updateBmeBcSub(BmeBcSubDTO bmeBcSubDTO) {
        return bmeBcSubDAO.updateBmeBcSub(bmeBcSubDTO);
    }

    /**
     * 删除bme_bc_sub基础表
     *
     * @param id
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public long delBmeBcSubById(String id) {
        return bmeBcSubDAO.delBmeBcSubById(id);
    }

    /**
     * 根据bme_bc_sub基础表标识获取bme_bc_sub基础表
     *
     * @param id
     * @return long
     * @author xhy
     * @date 2019/05/09 16:00
     */
    @Override
    public BmeBcSubDTO getBmeBcSubById(String id) {
        return bmeBcSubDAO.getBmeBcSubById(id);
    }

    @Override
    public CommPager<BmeBcSubDTO> findPageList(Map<Object, Object> condition) {
        int pageIndex = condition.get("pageIndex") == null ? 0 : Integer.valueOf(condition.get("pageIndex").toString());
        int pageSize = condition.get("pageSize") == null ? 10 : Integer.valueOf(condition.get("pageSize").toString());
        Page<BmeBcSubDTO> page = PageHelper.startPage(pageIndex, pageSize, true);
        List<BmeBcSubDTO> dictList = bmeBcSubDAO.getBmeBcSubList(condition);
        CommPager<BmeBcSubDTO> pageResult = new CommPager<>(page, dictList);
        return pageResult;
    }

    /**
     * 批量删除
     *
     * @param map
     * @return
     */
    @Override
    public Long batchDelete(Map<Object, Object> map) throws Exception {
        if (map.get("ids") == null) {
            throw new Exception("ids不能为空");
        }
        return bmeBcSubDAO.batchDelete((List<String>) map.get("ids"));
    }

    @Override
    public long enable(Map<Object, Object> map) throws Exception {
        if (map.get("id") == null || map.get("isEnable") == null) {
            throw new Exception("参数异常");
        }
        return bmeBcSubDAO.enable(map);
    }

    @Override
    public Long batchEnable(Map<Object, Object> map) throws Exception {
        if (map.get("ids") == null) {
            throw new Exception("ids不能为空");
        }
        return bmeBcSubDAO.batchEnable((List<Long>) map.get("ids"));
    }

    @Override
    public int importExcel(MultipartFile file) throws Exception {
        String rooPath = this.getClass().getResource("/").getPath();
        //复制文件到static目录
        String path = rooPath + "/static";
        String fileName = FileUploadUtils.uploadFile(file, path);
        String filePath = FileUploadUtils.uploadFile(file, path, fileName);
        FileInputStream fis = null;
        fis = new FileInputStream(filePath);
        // 创建工具类.
        ExcelUtil<BmeBcSubDTO> util = new ExcelUtil<BmeBcSubDTO>(BmeBcSubDTO.class);
        List<BmeBcSubDTO> paramList = util.importExcel(fileName, fis);
        HashMap<Object, Object> objectObjectHashMap = Maps.newHashMap();
        if (StringUtils.isEmpty(paramList)) {
            throw new Exception("数据为空！");
        }
        for (BmeBcSubDTO bmeBcSubDTO : paramList) {
            if (StringUtils.isEmpty(bmeBcSubDTO.getId())) {
                bmeBcSubDTO.setId(UuId.getShortUuid());
            } else {
                objectObjectHashMap.put("id", bmeBcSubDTO.getId());
                long count = bmeBcSubDAO.getBmeBcSubCount(objectObjectHashMap);
                if (count > 0) {
                    throw new Exception("数据已经存在！");
                }
            }
        }
        return bmeBcSubDAO.insertExcel(paramList);
    }

    @Override
    public void exportFile(Map<Object, Object> map, HttpServletResponse response) throws Exception {
        List<BmeBcSubDTO> list = bmeBcSubDAO.getBmeBcSubList(map);
        if (list.size() == 0) {
            throw new Exception("导出数据为空！");
        }
        // 创建工具类
        ExcelUtil<BmeBcSubDTO> util = new ExcelUtil<BmeBcSubDTO>(BmeBcSubDTO.class);
        // 导出
        String topName = DateUtil.getDateTime("yyyyMMddHHmmss");
        util.exportFile(list, DateUtil.getDateTime(topName), topName, response);
    }


}

